<template>
  <k-form-build ref="kfb" :value="processData.formJson" v-if="Object.keys(processData.formJson).length !== 0" :disabled="disabledAll" />
</template>

<script>
import { getFormByProcessDefinitionId, getFormDataByProcessInstanceId } from '@/api/process'
export default {
  name: 'taskKdf',
  data() {
    return {
      disabledAll: true,
      processData: {
        formJson: {},
        formData: {}
      }
    }
  },
  props: {
    taskData: {
      type: Object,
      default: () => {}
    }
  },
  methods: {
    async loadKDF() { // 根据任务信息获取kdf表单设计器数据及流程任务表单数据
      try {
        const formDataRes = await getFormDataByProcessInstanceId({ id: this.taskData.formDataId, processInstanceId: this.taskData.processInstanceId })
        if (!formDataRes.success) {
          return this.$message.error(formDataRes.message)
        }
        const formJsonRes = await getFormByProcessDefinitionId({ modelId: formDataRes.result.modelId, processDefinitionId: formDataRes.result.processDefinitionId})
        if (!formJsonRes.success) {
          return this.$message.error(formJsonRes.message)
        }
        this.processData.formData = JSON.parse(formDataRes.result.formData)
        this.processData.formJson = JSON.parse(formJsonRes.result.htmlJson)
      } catch (err) {
        console.log(err)
      }
    }
  },
  mounted() {
    this.loadKDF().then(res => {
      this.$nextTick(() => {
        this.$refs.kfb.form.setFieldsValue(this.processData.formData)
      })
    })
  }
}
</script>
